package org.taohuawu.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.taohuawu.pojo.User;
import org.taohuawu.service.UserService;
import org.taohuawu.web.util.JsonUtil;

import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Value("${user-service.users}")
    private String userJson;

    @Override
    public User getUser(Integer id) {
        List<User> list = JsonUtil.from(userJson, new TypeReference<List<User>>() {});
        for (User user : list) {
            if (user.getId() == id) {
                return user;
            }
        }
        return null;
    }

    @Override
    public void addScore(Integer id, Integer score) {
        log.info("user" + id + " 增加分数：" + score);
    }
}
